package com.i2bt.app.share;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.R;
import com.i2bt.adapter.changeShareAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.share.model.ChangeShareJsonHandle;
import com.i2bt.bean.ShareBean;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ShareChangeShareActivity extends BaseActivity {

	public String path;
	public ListView list;
	public Button save;
	public List<String> friendList;
	public List<String> friendId;
	public List<String> hasShareFriend;

	public CheckBox[] check;
	public boolean[] has;
	public boolean[] perState;
	public int index[];
	public changeShareAdapter adapter;

	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.share_change_my_share);
		Bundle bundle = this.getIntent().getExtras();
		path = bundle.getString("path");
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		boolean result = super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.share_change_menu, menu);
		this.setActionBarBack("返回");
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int id = item.getItemId();
		if (id == R.id.share_changeshare_select_all) {
			for (int i = 0; i < check.length; i++) {
				this.has[i] = true;
				this.adapter.notifyDataSetChanged();
			}
		} else if (id == R.id.share_changeshare_select_none) {
			for (int i = 0; i < check.length; i++) {
				this.has[i] = false;
				this.adapter.notifyDataSetChanged();
			}
		} else if (id == R.id.share_changeshare_select_otherside) {
			for (int i = 0; i < check.length; i++) {
				this.has[i] = !this.has[i];
				this.adapter.notifyDataSetChanged();
			}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0 == save) {
			checkShare();
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

		friendList = new ArrayList<String>();
		friendId = new ArrayList<String>();
		hasShareFriend = new ArrayList<String>();

		list = (ListView) this.findViewById(R.id.share_changeMyShare_list);
		save = (Button) this.findViewById(R.id.share_changeMyShare_btu);
		save.setOnClickListener(this);

		ShareBean bean = new ShareBean();
		bean.setAct(AndroidRestContract.SHOW_FRIEND);
		new ChangeShareJsonHandle(this).postTo(AndroidRestContract.REST_FRIEND,
				bean);
	}

	@Override
	protected void logic() {
		// TODO Auto-generated method stub
	}

	@Override
	public void backHomeOperate() {
		// TODO Auto-generated method stub
		finish();
	}

	public void checkShare() {
		int length = this.has.length;
		List<String> add = new ArrayList<String>();
		List<String> delete = new ArrayList<String>();
		for (int i = 0; i < length; i++) {
			if (this.has[i] != this.perState[i]) {
				if (this.perState[i]) {
					delete.add(friendId.get(i));
				} else {
					add.add(friendId.get(i));
				}
			}
		}
		if (add.size() == 0 && delete.size() == 0) {
			finish();
			System.out.println("没有改变!");
		} else {
			String a[] = new String[add.size()];
			String d[] = new String[delete.size()];
			for (int i = 0; i < add.size(); i++) {
				a[i] = add.get(i);
			}
			for (int i = 0; i < delete.size(); i++) {
				d[i] = delete.get(i);
			}
			ShareBean bean = new ShareBean();
			bean.setPath(path);
			bean.setAct(AndroidRestContract.CHANGE_SHARE);
			bean.setAddList(a);
			bean.setDeleteList(d);
			new ChangeShareJsonHandle(this).postTo(
					AndroidRestContract.REST_SHARED, bean);
		}
	}
}
